package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.AgentInfoMapper;
import cc.rengu.igas.bomp.common.entity.AgentInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代理商信息表操作接口实现类
 * Created by 王成 on 2020/3/10
 */
public class AgentInfoMapperImpl implements AgentInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertAgentInfo(AgentInfo agentInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertAgentInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, agentInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记代理商信息失败,agentCode:<{}>,agentName:<{}>,id:<{}>,iReturnCode:<{}>", agentInfo.getAgentCode(), agentInfo.getAgentName(), agentInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateAgentInfoById(AgentInfo agentInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateAgentInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, agentInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新代理商信息失败,agentCode:<{}>,agentName:<{}>,id:<{}>,iReturnCode:<{}>", agentInfo.getAgentCode(), agentInfo.getAgentName(), agentInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public AgentInfo selectAgentInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoById");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        agentInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, agentInfo, agentInfo);
        if (0 != iReturnCode) {
            rglog.error("获取代理商信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return agentInfo;
    }

    @Override
    public AgentInfo selectAgentInfoByPrimaryKey(String instId, String agentCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoByPrimaryKey");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        agentInfo.setAgentCode(agentCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, agentInfo, agentInfo);
        if (0 != iReturnCode) {
            rglog.error("获取代理商信息失败,instId:<{}>,agentCode:<{}>,iReturnCode:<{}>", instId, agentCode, iReturnCode);
            return null;
        }
        return agentInfo;
    }

    @Override
    public List<AgentInfo> selectAgentInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoByInstId");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, agentInfo, AgentInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取代理商信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (AgentInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectAgentInfoByInstId(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoByInstId");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, agentInfo, AgentInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取代理商信息失败,instId:<{}>", instId);
            return null;
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, agentInfo, AgentInfo.class, pageInfo);
    }

    @Override
    public List<AgentInfo> selectAgentInfoByAgentMchntNoOrAgentMchntNameOrSignInstId(String instId, String agentMchntNo, String agentMchntName, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoByAgentMchntNoOrAgentMchntNameOrSignInstId");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        if (null != agentMchntNo && !agentMchntNo.isEmpty()) {
            agentInfo.setAgentCode(agentMchntNo);
        }
        if (null != agentMchntName && !agentMchntName.isEmpty()) {
            agentInfo.setAgentName(agentMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            agentInfo.setManageInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, agentInfo, AgentInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据代理机构编号查询或代理机构名称模糊查询或根据签约机构代码查询信息失败,instId:<{}>,agentMchntNo:<{}>,agentMchntName:<{}>", instId, agentMchntNo, agentMchntName);
            return null;
        }
        return objectList.stream().map(item -> (AgentInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectAgentInfoByAgentMchntNoOrAgentMchntNameOrSignInstIdPageInfo(String instId, String agentMchntNo, String agentMchntName, String signInstId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentInfoByAgentMchntNoOrAgentMchntNameOrSignInstId");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        if (null != agentMchntNo && !agentMchntNo.isEmpty()) {
            agentInfo.setAgentCode(agentMchntNo);
        }
        if (null != agentMchntName && !agentMchntName.isEmpty()) {
            agentInfo.setAgentName(agentMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            agentInfo.setManageInstId(signInstId);
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, agentInfo, AgentInfo.class, pageInfo);
    }

    @Override
    public List<AgentInfo> bompSelectAgentInfoByManageInstIds(String instId, String agentMchntNo, String agentMchntName, String signInstId, String manageInstIds) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectAgentInfoByManageInstIds");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            agentInfo.setRemark1(manageInstIds);
        }
        if (!StringUtil.isEmptyOrNull(agentMchntNo)) {
            agentInfo.setAgentCode(agentMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(agentMchntName)) {
            agentInfo.setAgentName(agentMchntName);
        }
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            agentInfo.setManageInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, agentInfo, AgentInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据代理机构编号查询或代理机构名称模糊查询或根据签约机构代码查询信息失败,instId:<{}>", instId);
            return null;
        }
        List<AgentInfo> agentInfoList = objectList.stream().map(item -> (AgentInfo) item).collect(Collectors.toList());
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            List<String> manageInstIdList = Arrays.asList(manageInstIds.split(","));
            agentInfoList = agentInfoList.stream().filter(item -> manageInstIdList.contains(item.getManageInstId())).collect(Collectors.toList());
        }
        return agentInfoList;
    }

    @Override
    public PageResult bompSelectAgentInfoByPageAndManageInstIds(String instId, String agentMchntNo, String agentMchntName, String signInstId, String manageInstIds, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectAgentInfoByManageInstIds");
        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            agentInfo.setRemark1(manageInstIds);
        }
        if (!StringUtil.isEmptyOrNull(agentMchntNo)) {
            agentInfo.setAgentCode(agentMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(agentMchntName)) {
            agentInfo.setAgentName(agentMchntName);
        }
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            agentInfo.setManageInstId(signInstId);
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, agentInfo, AgentInfo.class, pageInfo);
    }
}
